package implicits

import scala.collection.mutable

/**
 * Created by Administrator on 2019/11/13
 */
trait MySeqTool {

  implicit class MySeq[T](seq: Seq[T]) {

    def distinctBy[B](f: T => B) = {
      seq.map { x =>
        (f(x), x)
      }.toMap.values.toList
    }

    def distinctByKeepHead[B](f: T => B) = {
      val map = mutable.Map[B, T]()
      seq.foreach { x =>
        val key = f(x)
        if (!map.isDefinedAt(key)) {
          map += (key -> x)
        }
      }
      map.values.toList
    }

  }


}
